package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.admin.dtos.SensitiveDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmSensitiveService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.Date;

/**
 * @author HYK
 * @date 2022/10/22
 */
@Service
@Transactional
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements WmSensitiveService {

    @Autowired
    private WmSensitiveMapper wmSensitiveMapper;

    /**
     * 查询敏感词列表
     *
     * @param dto dto
     * @return {@link ResponseResult}
     */
    @Override
    public ResponseResult list(SensitiveDto dto) {
        //查非空
        if (null == dto) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        dto.checkParam();
        //整合
        IPage<WmSensitive> page = new Page<>(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<WmSensitive> lqw = new LambdaQueryWrapper<>();
        //名字模糊查询
        if (StringUtils.isNotBlank(dto.getName())) {
            lqw.like(WmSensitive::getSensitives, dto.getName());
        }
        //创建时间倒序
        lqw.orderByDesc(WmSensitive::getCreatedTime);
        //查询
        page = page(page, lqw);
        //返回
        PageResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        result.setData(page.getRecords());
        return result;
    }

    /**
     * 新增敏感词
     *
     * @param wmSensitive wm敏感
     * @return {@link ResponseResult}
     */
    @Override
    public ResponseResult saveSensitive(WmSensitive wmSensitive) {
        if (null == wmSensitive || null == wmSensitive.getSensitives()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        WmSensitive selectOne = wmSensitiveMapper.selectOne(Wrappers.<WmSensitive>lambdaQuery()
                .eq(WmSensitive::getSensitives, wmSensitive.getSensitives()));
        if (null != selectOne) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        wmSensitive.setCreatedTime(new Date());
        save(wmSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 修改敏感词
     *
     * @param wmSensitive wm敏感
     * @return {@link ResponseResult}
     */
    @Override
    public ResponseResult updateSensitive(WmSensitive wmSensitive) {
        if (null == wmSensitive || null == wmSensitive.getSensitives() || null == wmSensitive.getId()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        WmSensitive selectOne = wmSensitiveMapper.selectOne(Wrappers.<WmSensitive>lambdaQuery()
                .eq(WmSensitive::getSensitives, wmSensitive.getSensitives()));
        if (null != selectOne) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        updateById(wmSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 删除敏感词
     *
     * @param id id
     * @return {@link ResponseResult}
     */
    @Override
    public ResponseResult deleteSensitive(Integer id) {
        if (null == id) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        int i = wmSensitiveMapper.deleteById(id);
        if (1 != i) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "删除失败");
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
